package edu.whut.mall.portal.controller;

import edu.whut.mall.common.api.CommonPage;
import edu.whut.mall.common.api.CommonResult;
import edu.whut.mall.portal.domain.PayInfoParams;
import edu.whut.mall.portal.dto.GroupBuyQualificationResult;
import edu.whut.mall.portal.service.IGroupBuyOrderService;
import edu.whut.mall.portal.service.IPortalGroupBuyService;
import edu.whut.mall.portal.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.security.NoSuchAlgorithmException;

@Slf4j
@RequiredArgsConstructor
@RequestMapping("/groupBuy")
@RestController
@Api(value="团购管理器",tags = {"团购管理器"})
public class RestGroupBuyController {

    private final IGroupBuyOrderService groupBuyOrderService;

    private final IPortalGroupBuyService portalGroupBuyService;


    @ApiOperation("团购用户下单")
    @PostMapping(value = "/createOneGroupBuyOrder")
    public CommonResult addOneGroupBuyOrder(@Valid @RequestBody PayInfoParams payInfoParams){
        GroupBuyOrderVo orderVo = groupBuyOrderService.createGroupBuyOrder(payInfoParams);
        if (orderVo == null) {
            return CommonResult.failed("添加团购订单失败");
        }
        return CommonResult.success(orderVo,"添加团购订单成功");
    }

    @ApiOperation("发起团购")
    @PostMapping(value = "/createGroupBuy")
    public CommonResult createGroupBuy(@NotNull @RequestParam Long userId,
                                       @NotNull @RequestParam Long groupBuyRuleId,
                                       @NotBlank @RequestParam String orderId) throws NoSuchAlgorithmException {
        GroupBuyQualificationResult result = portalGroupBuyService.createGroupBuyRecord(userId, groupBuyRuleId, orderId);
        if (!result.isQualified()) {
            log.info("失败的原因:{}", result.getFailMessage());
            return CommonResult.failed("发起团购失败:" + result.getFailMessage());
        }
        return CommonResult.success(result, "发起团购成功");
    }

    @ApiOperation("加入团购")
    @PostMapping(value = "/joinGroupBuy")
    public CommonResult joinGroupBuy(@NotNull @RequestParam Long userId,
                                     @NotNull @RequestParam Long groupBuyRecordId,
                                     @NotBlank @RequestParam String orderId) throws NoSuchAlgorithmException {
        GroupBuyQualificationResult result = portalGroupBuyService.joinGroupBuyRecord(userId, groupBuyRecordId, orderId);
        if (!result.isQualified()) {
            log.info("失败的原因:{}", result.getFailMessage());
            return CommonResult.failed("加入团购失败:" + result.getFailMessage());
        }
        return CommonResult.success(result, "加入团购成功");
    }


    @ApiOperation("查看是否能发起团购")
    @PostMapping(value = "/isQualifiedToCreateGroupBuyRecord")
    public CommonResult isQualifiedToCreateGroupBuyRecord(@NotNull @RequestParam Long userId,
                                                          @NotNull @RequestParam Long ruleId){
        GroupBuyQualificationResult result = portalGroupBuyService.isQualifiedToCreateGroupBuyRecord(userId, ruleId);
        if (!result.isQualified()) {
            return CommonResult.failed(result, "没有资格发起团购:" + result.getFailMessage());
        }
        return CommonResult.success(result, "可以发起团购");
    }

    @ApiOperation("查看是否能加入团购")
    @PostMapping(value = "/isQualifiedToJoinGroupBuyRecord")
    public CommonResult isQualifiedToJoinGroupBuyRecord(@NotNull @RequestParam Long userId,
                                                        @NotNull @RequestParam Long recordId,
                                                        @NotNull @RequestParam Integer ruleLimitNumber){
        GroupBuyQualificationResult result = portalGroupBuyService.isQualifiedToJoinGroupBuyRecord(userId, recordId, ruleLimitNumber);
        if (!result.isQualified()) {
            return CommonResult.failed(result, "没有资格发起团购:" + result.getFailMessage());
        }
        return CommonResult.success(result, "可以加入团购");
    }

    @ApiOperation("查看此次团购详情")
    @PostMapping(value = "/getGroupBuyRecordDetailByRecordId")
    public CommonResult getGroupBuyRecordDetailByRecordId(@NotNull @RequestParam Long recordId){
        GroupBuyRecordDetailVo groupBuyRecordDetailVo = portalGroupBuyService.getGroupBuyRecordDetailVoByRecordId(recordId);
        if (groupBuyRecordDetailVo == null) {
            return CommonResult.failed("获取团购订单信息失败");
        }
        return CommonResult.success(groupBuyRecordDetailVo, "获取团购订单信息成功");
    }

    @ApiOperation("获取所有团购分类")
    @PostMapping(value = "/listGroupBuyCategoryVos")
    public CommonResult listGroupBuyCategoryVos(@RequestParam(value="pageNum",defaultValue = "1") Integer pageNum,
                                                @RequestParam(value="size",defaultValue ="10") Integer size){
        CommonPage<GroupBuyCategoryVo> groupBuyCategoryVos = portalGroupBuyService.listGroupBuyCategoryVos(pageNum, size);
        if (groupBuyCategoryVos == null) {
            return CommonResult.failed("获取团购分类失败或者分类不存在");
        }
        return CommonResult.success(groupBuyCategoryVos,"获取团购分类成功");
    }

    @ApiOperation("根据团购规则id获取团购规则详情")
    @PostMapping(value = "/getGroupBuyRuleVosByRuleId")
    public CommonResult getGroupBuyRuleDetailVosByRuleId(@NotNull @RequestParam Long ruleId){
        GroupBuyRuleDetailVo groupBuyRuleDetailVo = portalGroupBuyService.getGroupBuyRuleDetailVosByRuleId(ruleId);
        if (groupBuyRuleDetailVo == null) {
            return CommonResult.failed("获取团购规则失败或者规则不存在");
        }
        return CommonResult.success(groupBuyRuleDetailVo,"获取团购规则成功");
    }


    @ApiOperation("根据团购分类id获取团购规则")
    @PostMapping(value = "/listGroupBuyRuleVosByCategoryId")
    public CommonResult listGroupBuyRuleVosByCategoryId(@NotNull @RequestParam Long categoryId,
                                                        @RequestParam(value="pageNum",defaultValue = "1") Integer pageNum,
                                                        @RequestParam(value="size",defaultValue ="10") Integer size){
        CommonPage<GroupBuyRuleVo> groupBuyRuleVos = portalGroupBuyService.listGroupBuyRuleVosByCategoryId(categoryId, pageNum, size);
        if (groupBuyRuleVos == null) {
            return CommonResult.failed("获取团购规则详情失败或者规则不存在");
        }
        return CommonResult.success(groupBuyRuleVos,"获取团购规则成功");
    }

    @ApiOperation("根据用户id和记录状态来获取团购记录")
    @PostMapping(value = "/getGroupBuyRecordVosByUserIdAndRecordStatus")
    public CommonResult getGroupBuyRecordVosByUserIdAndRecordStatus(@RequestParam(value="pageNum",defaultValue = "1") Integer pageNum,
                                                                    @RequestParam(value="size",defaultValue ="10") Integer size,
                                                                    @NotNull @RequestParam Long userId,
                                                                    @RequestParam(required = false) Integer status,
                                                                    @RequestParam(required = false) Integer userType){
        CommonPage<GroupBuyRecordVo> groupBuyRecordVoCommonPage = groupBuyOrderService.getGroupBuyRecordVosByUserIdAndRecordStatusAndUserType(pageNum, size, userId, status, userType);
        return CommonResult.success(groupBuyRecordVoCommonPage);
    }

    @ApiOperation("根据ruleId来获取正在进行的团购")
    @PostMapping(value = "/getGroupBuyRecordVoProcessingByRuleId")
    public CommonResult getGroupBuyRecordVoProcessingByRuleId(@RequestParam(value="pageNum",defaultValue = "1") Integer pageNum,
                                                              @RequestParam(value="size",defaultValue ="10") Integer size,
                                                              @NotNull @RequestParam Long ruleId,
                                                              @NotNull @RequestParam Integer status){
        CommonPage<GroupBuyRecordVo> groupBuyRecordVoCommonPage = groupBuyOrderService.getGroupBuyRecordProcessingVos(pageNum, size, ruleId, status);
        return CommonResult.success(groupBuyRecordVoCommonPage);
    }


    @ApiOperation("根据recordId和userId来获取订单详情")
    @PostMapping(value = "/getGroupBuyOrderVoByRecordIdAndUserId")
    public CommonResult getGroupBuyOrderVoByRecordIdAndUserId(@NotNull @RequestParam Long recordId,
                                                              @NotNull @RequestParam Long userId){
        return CommonResult.success(groupBuyOrderService.getGroupBuyOrderVoByRecordIdAndUserId(recordId, userId));
    }


    @ApiOperation("获取商品分类")
    @PostMapping(value = "/listProductCategoryVos")
    public CommonResult listProductCategoryVos(){
        return CommonResult.success(portalGroupBuyService.listProductCategoryVos(), "获取商品分类成功");
    }

    @ApiOperation("根据商品分类获取团购规则")
    @PostMapping(value = "/listGroupBuyRuleVosByProductCategory")
    public CommonResult listGroupBuyRuleVosByProductCategory(@NotNull @RequestParam Long productCategoryId){
        return CommonResult.success(portalGroupBuyService.listGroupBuyRuleVosByProductCategory(productCategoryId), "获取团购规则成功");
    }

    @ApiOperation("根据商品名称来搜索团购规则")
    @PostMapping(value = "/getGroupBuyRuleVoBySearchingName")
    public CommonResult getGroupBuyRuleVoBySearchingName(@RequestParam String productName){
        return CommonResult.success(portalGroupBuyService.getGroupBuyRuleVoBySearchingName(productName), "获取团购规则成功");
    }


}